package com.zsr.statement.crud;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

public class StatementTest {

    //使用 Statement的弊端：需要sql拼写，并存在SQL注入问题
    @Test
    public void testLogin(){
        Scanner scan = new Scanner(System.in);
        System.out.print("用户名：");
        String userName = scan.nextLine();
        System.out.print("密 码：");
        String password = scan.nextLine();
        String sql = "SELECT user,password FROM user_table WHERE USER = '" + userName
                + "' AND PASSWORD = '" + password
                + "'";
        User user = get(sql, User.class);
        if (user != null) {
            System.out.println("登陆成功!");
        } else {
            System.out.println("用户名或密码错误！");
        }


    }

    public <T> T get(String sql,Class<T> clazz){
        T t=null;

        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;

        try {
            //1.加载配置文件
            InputStream is=StatementTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties pros=new Properties();
            pros.load(is);

            //2.读取 配置信息
            String user=pros.getProperty("user");
            String password=pros.getProperty("password");
            String url=pros.getProperty("url");
            String driverClass =pros.getProperty("driverClass");

            //3.加载驱动
            Class.forName(driverClass);

            //4.获取连接
            conn= DriverManager.getConnection(url,user,password);

            st=conn.createStatement();

            rs=st.executeQuery(sql);

            //获取结果集的元数据
            ResultSetMetaData rsmd=rs.getMetaData();

            //获取结果集的列数
            int columnCount = rsmd.getColumnCount();

            if(rs.next()){
                t =clazz.newInstance();

                for(int i=0;i<columnCount;i++){
                    //1.获取列的名称
                    String columnName=rsmd.getColumnLabel(i+1);

                    //2.根据列名获取对应数据表中的数据
                    Object columnVal = rs.getObject(columnName);

                    // 3. 将数据表中得到的数据，封装进对象
                    Field field = clazz.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(t, columnVal);

                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
// 关闭资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
